/*
 * CustomerAction.java
 * 
 * Created on 02/10/2007, 12:43:14 PM
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.actions;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.bean.CedulaVotacion;
import model.bean.ListaCandidatos;
import model.bean.MiembroLista;
import model.bean.PadronElectoral;
import model.bean.Socio;
import model.bean.Usuario;

import model.factory.RecreacionalFactory;
import model.service.CedulaVotacionService;
import model.service.MiembroListaService;
import model.service.PadronElectoralService;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import view.common.Constants;
import view.common.struts.CustomActionForm;

import view.common.struts.CustomDispatchAction;

/**
 * @Archivo LoginAction.java
 * @Fecha                @Autor   @Cambios
 * 21/01/2011 02:35:29 PM       moshiro
 */
public class VotacionAction extends CustomDispatchAction {

    private final static String PRINCIPAL = "principal";

    @Override
    public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        MiembroLista beanSearch = new MiembroLista();
        frm.setBeanList(getMiembroListaService().listarVotacion(beanSearch));
        frm.setMapList(new HashMap());
        Socio socio = ((Usuario) req.getSession().getAttribute(Constants.SESSION_USER)).getSocio();
        if (socio != null) {
            List<PadronElectoral> lista = getPadronElectoralService().listarPadronElectoral(new PadronElectoral(socio));
            frm.getMapList().put("voto", (lista.isEmpty() || lista.get(0).getEstado() == 0) ? 0 : 1);
            frm.getMapList().put("padron", !lista.isEmpty() ? lista.get(0) : null);
        }
        return mapping.findForward(PRINCIPAL);
    }

    public ActionForward guardar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        System.out.println("req.getRemoteAddr():" + req.getRemoteAddr());
        System.out.println("req.getRemoteHost():" + req.getRemoteHost());
        CedulaVotacion bean = new CedulaVotacion();
        bean.setNombreComputador(req.getRemoteAddr());
        bean.setListaCandidatos(new ListaCandidatos(Long.parseLong(req.getParameter("codigo"))));
        getCedulaVotacionService().agregarCedulaVotacion(bean);

        PadronElectoral pPadron = (PadronElectoral) frm.getMapList().get("padron");
        if (pPadron != null) {
            PadronElectoral padron = new PadronElectoral();
            padron.setCodigo(pPadron.getCodigo());
            padron.setEstado(1);
            getPadronElectoralService().modificarPadronElectoral(padron);
        }
        return mapping.findForward("home");
    }

    public MiembroListaService getMiembroListaService() {
        return RecreacionalFactory.getInstance().getMiembroListaService();
    }

    public CedulaVotacionService getCedulaVotacionService() {
        return RecreacionalFactory.getInstance().getCedulaVotacionService();
    }

    private PadronElectoralService getPadronElectoralService() {
        return RecreacionalFactory.getInstance().getPadronElectoralService();
    }
}
